/*
 * Copyright (C) 2014 Lucid Wolf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.lucidhex.hex;

/**
 *
 * @author Wolf
 */
public class LucidIndex implements Comparable{


    private int r = 0;
    private int q = 0;
    public LucidIndex(int q, int r){
        this.q = q;
        this.r = r;
    }
    public LucidIndex(int a, int b, int c){
        this.q = a;
        this.r = c;
    }
    public LucidIndex(LucidCoord eC){
        r = (int)Math.round(eC.R());
        q = (int)Math.round(eC.Q());
    }
    public LucidIndex(PlaneCoord sC){
        LucidCoord eC = new LucidCoord(sC);
        r = (int)Math.round(eC.R());
        q = (int)Math.round(eC.Q());
    }
    public LucidIndex(String hexToString){
        String coord = hexToString.substring(hexToString.indexOf("@")).trim();
        String[] values = coord.split(",");
        this.q = Integer.parseInt(values[0].trim());
        this.r = Integer.parseInt(values[2].trim());
    }
    public int R(){
        return r;
    }
    public int Q(){
        return q;
    }
    public int A(){
        return q;
    }
    public int B(){
        return -q-r;
    }
    public int C(){
        return r;
    }
    @Override
    public int compareTo(Object aObject) {
        // exception could happen here
        LucidIndex aCoord = (LucidIndex)aObject;
        // go by columns then rows
        int out = aCoord.Q() - q;
        if(out == 0){
            out = aCoord.R() - r;
        }            
        return out;
    }
    @Override
    public boolean equals(Object o){
        return (compareTo(o) == 0);
    }
    @Override
    public String toString(){
        return A()+","+B()+","+C();
        
    }
    @Override
    public int hashCode(){
        return toString().hashCode();
    }
    public int[] toArray() {
        int[] out = new int[2];
        out[0] = q;
        out[1] = r;
        return out;
    }
    public static int distance (LucidIndex c1, LucidIndex c2){
        return Math.max(Math.abs(c1.A()-c2.A()), Math.max(Math.abs(c1.B()-c2.B()),Math.abs(c1.C()-c2.C())));
    }
}
